/**-------------------------------------------------------------------------------------------
** Created by:          qinyx
** Last modified Date:  2014-02-28
** Last Version:        V1.00
** Descriptions:        STM32F407嵌入式实验教学箱
**	  Gpio配置文件
**
**-------------------------------------------------------------------------------------------*/
#include "stm32f4xx.h"
#include "trafficlight.h"
#include "delay.h"
#include "lcd.h"
#include "stdio.h"
#include "DTH11.h"
#include "BMP180.h"
#include "KEY.h"
#include "test.h"  // 添加test.h引用，以使用字符串宏定义


/*********************************************************************************************************
函数名称：void LEDGpio_Init(void)
入口参数：无
出口参数：无
//LEDR_NS     PB0
//LEDY_NS     PA3
//LEDG_NS     PA5

//LEDR_EW     PF13
//LEDY_EW     PF14
//LEDG_EW     PF11
**********************************************************************************************************/
void TrafficLightGpio_Init(void)
{    
    GPIO_InitTypeDef  GPIO_InitStructure;
    
    //LEDR_NS(PB0)
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
    GPIO_Init(GPIOB, &GPIO_InitStructure);
    
    //  LEDY_NS(PA3)和LEDG_NS(PA5)
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_5;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    
    //LEDR_EW(PF13)、LEDY_EW(PF14)、LEDG_EW(PF11)
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_11;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
    GPIO_Init(GPIOF, &GPIO_InitStructure);
    
    LEDR_NS_OFF;
    LEDY_NS_OFF;
    LEDG_NS_OFF;
    
    LEDR_EW_OFF;
    LEDY_EW_OFF;
    LEDG_EW_OFF;
}


/*********************************************************************************************************
函数名称：TrafficLight_Run
入口参数：无
出口参数：无
**********************************************************************************************************/

unsigned char TrafficLight_Run(void)
{   
    unsigned char value = 0;
    static unsigned char last_state = 0;  // 上一个状态

    // 获取传感器数据：包括温湿度和气压
    unsigned char temperature;          
    unsigned char humidity;     
    char buf[20] = "";
    long Databuf[2] = {0};
    char Dispbuf[50] = "";

    // 读取温湿度和气压数据
    DTH11_ReadData(&temperature, &humidity);  // 读取温湿度
    BMP180Convert(Databuf);  // 读取气压数据

    // 初始化信号灯，显示当前状态
    if (last_state == 0) {  // 程序初始时
        LEDY_NS_ON;
        LEDY_EW_ON;
        LEDR_NS_OFF;
        LEDR_EW_OFF;
        LEDG_NS_OFF;
        LEDG_EW_OFF;

        // 屏幕显示提示信息
        lcd_clear(Black);
        LCD_DrawRectangle(0,0, 479, 271, Green);
        LCD_DrawRectangle(5,5, 474, 266, Green);			
        LCD_ShowString(get_centered_x(STR_SMART_HOME), 50, STR_SMART_HOME, White, Black);    
        LCD_ShowString(get_centered_x(STR_WEATHER_TITLE), 100, STR_WEATHER_TITLE, Red, Black);	
        LCD_ShowString(get_centered_x(STR_S1_END), 225, STR_S1_END, White, Black); 
        LCD_ShowString(get_centered_x(STR_CLOUDY_ADVICE), 200, STR_CLOUDY_ADVICE, Yellow, Black);
        last_state = 3;  // 设置状态为阴天
    }

    // 判断天气状态，控制灯光
    else if (Databuf[1] / 1000.0 < 103 && humidity >= 70)  // 雨天条件
    {
        // 设置红灯亮起
        LEDR_NS_ON;
        LEDR_EW_ON;
        LEDG_NS_OFF;
        LEDG_EW_OFF;
        LEDY_NS_OFF;
        LEDY_EW_OFF;

        // 屏幕显示天气情况
        if (last_state != 1) {  // 如果状态发生改变
            lcd_clear(Black);  // 只在状态改变时清屏
            LCD_DrawRectangle(0,0, 479, 271, Green);
            LCD_DrawRectangle(5,5, 474, 266, Green);
            LCD_ShowString(get_centered_x(STR_SMART_HOME), 50, STR_SMART_HOME, White, Black);    
            LCD_ShowString(get_centered_x(STR_WEATHER_TITLE), 100, STR_WEATHER_TITLE, Red, Black);	
            LCD_ShowString(get_centered_x(STR_S1_END), 225, STR_S1_END, White, Black); 
            LCD_ShowString(get_centered_x(STR_RAINY_ADVICE), 200, STR_RAINY_ADVICE, Red, Black);
            last_state = 1;  // 更新状态
        }
    }
    else if (Databuf[1] / 1000.0 >= 105 && humidity <= 40)  // 晴天条件
    {
        // 设置绿灯亮起
        LEDG_NS_ON;
        LEDG_EW_ON;
        LEDR_NS_OFF;
        LEDR_EW_OFF;
        LEDY_NS_OFF;
        LEDY_EW_OFF;

        // 屏幕显示天气情况
        if (last_state != 2) {  // 如果状态发生改变
            lcd_clear(Black);  // 只在状态改变时清屏
            LCD_DrawRectangle(0,0, 479, 271, Green);
            LCD_DrawRectangle(5,5, 474, 266, Green);
            LCD_ShowString(get_centered_x(STR_SMART_HOME), 50, STR_SMART_HOME, White, Black);    
            LCD_ShowString(get_centered_x(STR_WEATHER_TITLE), 100, STR_WEATHER_TITLE, Red, Black);	
            LCD_ShowString(get_centered_x(STR_S1_END), 225, STR_S1_END, White, Black); 
            LCD_ShowString(get_centered_x(STR_SUNNY_ADVICE), 200, STR_SUNNY_ADVICE, Green, Black);
            last_state = 2;  // 更新状态
        }
    }
    else  // 阴天条件 (其他所有情况都视为阴天)
    {
        // 设置黄灯亮起
        LEDY_NS_ON;
        LEDY_EW_ON;
        LEDR_NS_OFF;
        LEDR_EW_OFF;
        LEDG_NS_OFF;
        LEDG_EW_OFF;
			
        // 屏幕显示提示信息
        if (last_state != 3) { 
            lcd_clear(Black);
            LCD_DrawRectangle(0,0, 479, 271, Green);
            LCD_DrawRectangle(5,5, 474, 266, Green);			
            LCD_ShowString(get_centered_x(STR_SMART_HOME), 50, STR_SMART_HOME, White, Black);    
            LCD_ShowString(get_centered_x(STR_WEATHER_TITLE), 100, STR_WEATHER_TITLE, Red, Black);	
            LCD_ShowString(get_centered_x(STR_S1_END), 225, STR_S1_END, White, Black); 
            LCD_ShowString(get_centered_x(STR_CLOUDY_ADVICE), 200, STR_CLOUDY_ADVICE, Yellow, Black);
            last_state = 3;  // 设置状态为阴天
        }
    }

    // **实时更新温湿度和气压数据**
    sprintf(buf, STR_TEMPERATURE, temperature);
    LCD_ShowString(60, 150, (unsigned char*)buf, White, Black);
    sprintf(buf, STR_HUMIDITY, humidity);
    LCD_ShowString(150, 150, (unsigned char*)buf, White, Black);
    sprintf(Dispbuf, STR_PRESSURE, Databuf[1] / 1000.0);
    LCD_ShowString(280, 150, (unsigned char*)Dispbuf, White, Black);
    
    // 检查按下按键S3后退出程序
    if (KeyScan() == 3)
    {                
        value = 1;
    }
    return value;
}
